﻿
#include "FrameWindow.h"
#include "Resource.h"
#include "AppItem.h"

#define __IMenuPopup_FWD_DEFINED__
#include "Shlobj.h"

#define UWM_ADD_APPITEM       (UWM_USER + 1)
#define UWM_UPDATE_CLIENT     (UWM_USER + 2)

BOOL AddAppItem(HWND hWnd, LPCTSTR strFloder)
{
	// 构建目录路径
	TCHAR strPath[MAX_PATH];
	int nLenght = wcslen(strFloder);
	wcscpy_s(strPath, MAX_PATH, strFloder);
	wcscpy_s(&strPath[nLenght], MAX_PATH - nLenght, _T("*"));
	// 初始化 WIN32_FIND_DATA 结构体
	WIN32_FIND_DATA FindFileData = { 0 };
	HANDLE hFind = FindFirstFile(strPath, &FindFileData);
			
	if (hFind == INVALID_HANDLE_VALUE) {
		return FALSE;
	}

	// 遍历文件
	do {
		if (INVALID_FILE_ATTRIBUTES == FindFileData.dwFileAttributes
			|| FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
		{
			continue;
		}

		wcscpy_s(&strPath[nLenght], MAX_PATH - nLenght, FindFileData.cFileName);
		CAppItem* pItem = new CAppItem();
		pItem->SetApp(strPath);

		SendMessage(hWnd, UWM_ADD_APPITEM, 0, (LPARAM)pItem);

	} while (FindNextFile(hFind, &FindFileData) != 0);

	// 关闭句柄
	FindClose(hFind);
}

DWORD WINAPI CreateAllItem(LPVOID lParam)
{
	HWND hWnd = (HWND)lParam;
	WCHAR strFolder[MAX_PATH];
	::CoInitialize(NULL);

	if (SHGetSpecialFolderPathW(NULL, strFolder, CSIDL_DESKTOPDIRECTORY, FALSE))
	{
		wcscat_s(strFolder, MAX_PATH, _T("\\"));
		AddAppItem(hWnd, strFolder);
	}

	if (SHGetSpecialFolderPathW(NULL, strFolder, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE))
	{
		wcscat_s(strFolder, MAX_PATH, _T("\\"));
		AddAppItem(hWnd, strFolder);
	}

	::CoUninitialize();
	//::SendMessage(hWnd, UWM_UPDATE_CLIENT, 0, 0);
	return 0;
}

CFrameWindow::CFrameWindow()
{
	m_pMenUI = NULL;
	m_pClient = NULL;
};

CFrameWindow::~CFrameWindow()
{
};

void CFrameWindow::OnNotify(MUINOTIFY &notify)
{
	//想知道wParam, lParam对应了什么参数吗？说实话，由于项目还处于初期，
	//我也说不准wParam, lParam到底对应了什么参数，自己在断点中拦截到对应
	//的通知后，看[调用堆栈]追踪一下吧。
	CControlUI * pControl = (CControlUI*) notify.pSender;
	switch(notify.dwType)
	{
	case EnumNotify::ClickItem:
	{
		if (pControl->GetClassName() == AppItemClassName)
		{
			CAppItem* pItem = dynamic_cast<CAppItem*>(pControl);
			if (pItem)
			{
				pItem->RunApp();
			}
		}
        else if (pControl->GetName() == L"btnOK")
	    {
			//OnClick_Login();
		}
	}break;
	case EnumNotify::ActiveItem:
		{
			pControl = (CControlUI*)notify.lParam;
			//MessageBox(m_hWnd, pConntrol->GetClassName(), pConntrol->GetText(), NULL);
		}break;
	case EnumNotify::TimerCall:
		{

		}break;
	case EnumNotify::CheckItem:
		{

		}break;
	}

	//return __super::OnNotify(notify);
};

LRESULT CFrameWindow::OnEvent(MUIEVENT &event)
{
	CDragItemUI * pDragItem = NULL;
	switch (event.dwType)
	{
	case EnumEvent::WindowInit:
		{
			OnCreate(event.wParam, event.lParam);
			//SetTimer(m_hWnd, 100, 5000, NULL);
		}break;
	case EnumEvent::WindowReady:
		{
			OnReady(event.wParam, event.lParam);
		}break;
	case EnumEvent::DragOver:
		{
			
		}break;
	case EnumEvent::OnTimer:
		{

		}break;
	case EnumEvent::WindowDestroy:
	{
		if (m_pMenUI)
		{
			m_pMenUI->Destroy();
			delete m_pMenUI;
			m_pMenUI = NULL;
		}

	}break;
	default:
		break;
	}

	return 0;
	//return __super::OnEvent(event);
}

LRESULT CALLBACK CFrameWindow::WndProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	CControlUI * pControl = NULL;
	POINT point;
	CDragItemUI * pDragItem = NULL;
	SIZE szRound = {10,10};
	BYTE keyState[256] = { 0 };
	switch (message)
	{
	case UWM_ADD_APPITEM:
	{
		CAppItem* pItem = reinterpret_cast<CAppItem*>(lParam);
		if (m_pClient && pItem)
		{
			m_pClient->Add(pItem);
			m_pClient->Update();
			return TRUE;
		}
		return FALSE;
	}break;
	case UWM_UPDATE_CLIENT:
	{
		if (m_pClient)
		{
			m_pClient->Update();
		}
		return TRUE;
	}break;
	case WM_LBUTTONDOWN:
		{
			point.x = GET_X_LPARAM(lParam);
			point.y = GET_Y_LPARAM(lParam);
		}break;
	case WM_MOUSEWHEEL:
	{
		if (FALSE == GetKeyboardState(keyState))
		{
			break;
		}

		if (keyState[VK_CONTROL] & 128)
		{
			int nRatio = this->GetDpiRatio();
			int nShift = (short)HIWORD(wParam);

			nRatio += nShift / 30;
			this->SetDpiRatio(nRatio);
			::UpdateWindow(m_hWnd);
			::InvalidateRect(m_hWnd, NULL, FALSE);
			return 0;
		}
		
	}break;
	default:
		break;
	}
	return __super::WndProc(message, wParam, lParam);
}


LRESULT CFrameWindow::OnCreate(WPARAM wParam, LPARAM lParam)
{
	TCHAR strSkin[MAX_PATH] = {0};
	wsprintf(strSkin, _T("file='%s'"), _T("skin/"));
	SetSkin(strSkin);

	CBuilder* pBuilder = new CBuilder(this, nullptr);
	CControlUI* pRootLayout = pBuilder->Create(strSkin, _T("desktop.xml"));
	delete pBuilder;
	this->AttachFrameView(pRootLayout);

	m_pClient = (CTileLayoutUI*)FindControl(_T("vClient"));
	//m_pMenUI = new CMenuUI();
	//m_pMenUI->Create(m_hInstance, _T("MYMENU"), strSkin, _T("menu.xml"));

	CreateThread(NULL, 0, CreateAllItem, m_hWnd, 0, NULL);
	return 0;
}

LRESULT CFrameWindow::OnReady(WPARAM wParam, LPARAM lParam)
{

	return 0;
}

BOOL CFrameWindow::AddAppItem(LPCTSTR strFloder)
{
	return TRUE;
}